package com.socket.netty;


import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;

/**
 * Created by kaddddd on 2017/7/4 0004.
 */
public class LoginSocketPackage {

    /*//头标识
    private final String headerFlag = "0x5b";
    //数据头
    private String massageHeader;
    //登录请求信息
    private String loginRequest;
    //CRC校验码
    private String crcCode;
    //尾标识
    private final String endFlag = "0x5d";

    public String getHeaderFlag() {
        return headerFlag;
    }

    public String getMassageHeader() {
        return massageHeader;
    }

    public void setMassageHeader(String massageHeader) {
        this.massageHeader = massageHeader;
    }

    public String getLoginRequest() {
        return loginRequest;
    }

    public void setLoginRequest(String loginRequest) {
        this.loginRequest = loginRequest;
    }

    public String getCrcCode() {
        return crcCode;
    }

    public void setCrcCode(String crcCode) {
        this.crcCode = crcCode;
    }

    public String getEndFlag() {
        return endFlag;
    }

    //提供序列化方法，对实体类进行序列化，转换为byte数组
    public byte[] getByteStream() throws UnsupportedEncodingException {
        byte[] massageHeaderBytes = this.massageHeader.getBytes("gbk");//获得字节数组
        int massageHeaderLength = massageHeaderBytes.length;

        byte[] loginRequestBytes = this.loginRequest.getBytes("gbk");//获得字节数组
        int loginRequestLength = loginRequestBytes.length;

        int socketLength = 4 + massageHeaderLength + loginRequestLength + 2 +4;
        byte [] soc = new byte[socketLength];
        //添加校验数据
        int index = 0;
        soc[0]=0x11;
        soc[1]=0x12;
        soc[2]=0x13;
        index+=3;
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(8);
        numberFormat.setGroupingUsed(false);
        byte [] num = numberFormat.format(socketLength).getBytes();
        for(int i = 0;i<8;i++){
            soc[index++]= num[i];
        }
        //添加loginRequest内容
        for(int i = 0;i<loginRequestLength;i++){
            soc[index++] = loginRequestBytes[i];
        }

        return soc;
    }

    //字节装转报文string
    public String getString(byte [] socketBytes){
        String syncStr = this.bytesToString(socketBytes, 0, 3);
        String socketLength = this.bytesToString(socketBytes, 3, 3+8);
        String body = this.bytesToString(socketBytes, 3+8, socketBytes.length-32);
        String md5 = this.bytesToString(socketBytes,socketBytes.length-32,socketBytes.length);
        return syncStr+socketLength+body+md5;
    }

    //将字节数组转化为string
    public String bytesToString(byte [] bytes,int start,int end){
        String str = "";
        if(bytes.length<end-start){
            return str;
        }
        byte [] bs = new byte[end-start];
        for(int i = 0;i<end-start;i++){
            bs[i] = bytes[start++];
        }
        str = new String(bs);
        return str;
    }

    public String toString(){
        return this.headerFlag+this.massageHeader+this.loginRequest+this.md5;
    }
*/
}
